package com.hucsmn.studentms;

import java.util.*;
import java.util.regex.Pattern;

/**
 * 作业题四：
 * 使用 List 集合实现简易的学生信息管理系统，要求打印字符界面提示用户选择相应的功能，
 * 根据用户输入的选择去实现增加、删除、修改、查找以及遍历所有学生信息的功能。
 * 其中学生的信息有：学号、姓名、年龄。
 * 要求：尽量将功能拆分为多个 .java 文件。
 */
public class ManageSystem {
    private static final Pattern REGEX_SEPARATOR = Pattern.compile("[ \n\r\t]+");
    private static final Scanner scanner = new Scanner(System.in);
    private static final Database db = new Database();

    public static void main(String[] args) {
        System.out.println("简易学生信息管理系统");
        System.out.println();
        help();

        while (true) {
            prompt();
        }
    }

    private static void prompt() {
        System.out.print("学生信息管理系统> ");
        String[] cmd = parseLine(scanner.nextLine());
        if (cmd.length == 0) {
            help();
            return;
        }
        switch (cmd[0].toLowerCase()) {
            case "a":
            case "add":
            case "添加":
                add(cmd);
                break;
            case "d":
            case "del":
            case "delete":
            case "删除":
                delete(cmd);
                break;
            case "u":
            case "update":
            case "修改":
                update(cmd);
                break;
            case "g":
            case "lookup":
            case "get":
            case "查找":
                get(cmd);
                break;
            case "l":
            case "list":
            case "显示":
                list(cmd);
                break;
            case "q":
            case "quit":
            case "exit":
            case "退出":
                System.exit(0);
                break;
            default:
                help();
                break;
        }
    }

    private static String[] parseLine(String line) {
        return REGEX_SEPARATOR.split(line.trim());
    }

    private static void help() {
        System.out.println("命令：");
        System.out.println("  添加 学号 姓名 年龄");
        System.out.println("  删除 学号");
        System.out.println("  修改 学号 [新的学号] 姓名 年龄");
        System.out.println("  修改 学号 新的学号");
        System.out.println("  查找 学号");
        System.out.println("  显示");
        System.out.println("  退出");
        System.out.println();
    }

    private static void add(String[] cmd) {
        Student student;
        try {
            if (cmd.length != 4) {
                throw new IllegalArgumentException();
            }
            int number = Integer.parseInt(cmd[1]);
            String name = cmd[2];
            int age = Integer.parseInt(cmd[3]);
            student = new Student(number, name, age);
        } catch (NumberFormatException ex) {
            System.out.println("学号和年龄必须是数字！");
            return;
        } catch (IllegalArgumentException ex) {
            System.out.println("参数不正确！");
            return;
        }

        if (!db.add(student)) {
            System.out.println("学号重复！");
        } else {
            System.out.println("信息已录入。");
        }
    }

    private static void delete(String[] cmd) {
        int number;
        try {
            if (cmd.length != 2) {
                throw new IllegalArgumentException();
            }
            number = Integer.parseInt(cmd[1]);
        } catch (NumberFormatException ex) {
            System.out.println("学号必须是数字！");
            return;
        } catch (IllegalArgumentException ex) {
            System.out.println("参数不正确！");
            return;
        }

        if (!db.remove(number)) {
            System.out.println("学号不存在！");
        } else {
            System.out.println("信息已删除。");
        }
    }

    private static void update(String[] cmd) {
        int number;
        Student student;
        try {
            if (cmd.length == 3) {
                number = Integer.parseInt(cmd[1]);
                int newNumber = Integer.parseInt(cmd[2]);
                Student old = db.get(number);
                if (old == null) {
                    throw new NoSuchElementException();
                }
                student = new Student(newNumber, old.getName(), old.getAge());
            } else if (cmd.length == 4) {
                number = Integer.parseInt(cmd[1]);
                String name = cmd[2];
                int age = Integer.parseInt(cmd[3]);
                student = new Student(number, name, age);
            } else if (cmd.length == 5) {
                number = Integer.parseInt(cmd[1]);
                int newNumber = Integer.parseInt(cmd[2]);
                String name = cmd[3];
                int age = Integer.parseInt(cmd[4]);
                student = new Student(newNumber, name, age);
            } else {
                throw new IllegalArgumentException();
            }
        } catch (NumberFormatException ex) {
            System.out.println("学号和年龄必须是数字！");
            return;
        } catch (NoSuchElementException ex) {
            System.out.println("学号不存在！");
            return;
        } catch (IllegalArgumentException ex) {
            System.out.println("参数不正确！");
            return;
        }

        if (db.update(number, student) == null) {
            System.out.println("学号不存在或新的学号重复！");
        } else {
            System.out.println("信息已修改。");
        }
    }

    private static void get(String[] cmd) {
        int number;
        try {
            if (cmd.length != 2) {
                throw new IllegalArgumentException();
            }
            number = Integer.parseInt(cmd[1]);
        } catch (NumberFormatException ex) {
            System.out.println("学号必须是数字！");
            return;
        } catch (IllegalArgumentException ex) {
            System.out.println("参数不正确！");
            return;
        }

        Student student;
        if ((student = db.get(number)) == null) {
            System.out.println("学号不存在！");
        } else {
            System.out.println("学生信息为：");
            System.out.println(student);
            System.out.println();
        }
    }

    private static void list(String[] cmd) {
        if (cmd.length != 1) {
            System.out.println("不需要提供参数！");
            return;
        }

        System.out.println("已录入的学生信息：");
        for (Student student : db) {
            System.out.println(student);
        }
        System.out.println();
    }
}
